package com.example.sls.controller.Class;

import com.example.sls.pojo.Class._Class;
import com.example.sls.service.Class.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/class")
public class ClassController {

    @Autowired
    ClassService classService;

    @GetMapping("/getInfoByCId")
    public _Class getClassInfoByCId(@RequestParam Map<String, String> params){
//        System.out.println("========");
//        System.out.println("params: " + params);
//        System.out.println("========");
        Integer CId = Integer.valueOf(params.get("CId"));
        return classService.getClassInfoByCId(CId);
    }

    @PostMapping("/addClass")
    public Map<String, String> addClass(@RequestParam Map<String, String> params){
        Map<String, String> res = new HashMap<>();

        Map<String, Object>map = new HashMap<>();

        Integer id = classService.getMaxId() + 1;
        Integer classId = Integer.valueOf(params.get("classId"));
        Integer tId = Integer.valueOf(params.get("tId"));
        Integer cnt = Integer.valueOf(params.get("cnt"));

//        System.out.println("========");
//        System.out.println(id);
//        System.out.println(classId);
//        System.out.println(tId);
//        System.out.println(cnt);
//        System.out.println("========");

        map.put("id", id);
        map.put("classId", classId);
        map.put("tId",tId);
        map.put("cnt",cnt);

        if(classService.getClassInfoByCId(classId) != null){
            res.put("error_message", "classId已存在");
            return res;
        }

        if(classService.addClass(map)){
            res.put("error_message", "success");
        }
        else{
            res.put("error_message", "failed");
        }

        return res;
    }
}
